//
//  HomeViewController.m
//  Product
//
//  Created by 吴志勇 on 2020/4/2.
//  Copyright © 2020 wzy. All rights reserved.
//

#import "HomeViewController.h"
#import "TableViewCell.h"
#import "HomeView.h"
#import "YJDataViewModel.h"
#import "MJRefresh.h"

#define statusHeight ([UIApplication sharedApplication].statusBarFrame.size.height)
#define navH (self.navigationController.navigationBar.bounds.size.height)
#define WS(weakSelf)    __weak __typeof(&*self) weakSelf = self

@interface HomeViewController ()

/*  */
@property (nonatomic, strong) HomeView *homeView;
/*  */
@property (nonatomic, strong) YJDataViewModel *dataViewModel;

@end

@implementation HomeViewController

static NSString *cellID = @"YTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self setData];
    
    
}

- (void)setData{
    WS(weakSelf);
    [self.dataViewModel requestData:^{
        [weakSelf.homeView.tableView reloadData];
    } fBlock:^{
        
    } isRefresh:NO];
    
    self.homeView.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf.dataViewModel requestData:^{
            [weakSelf.homeView.tableView.mj_header endRefreshing];
            [weakSelf.homeView.tableView reloadData];
        } fBlock:^{
            
        } isRefresh:YES];
    }];
    
    self.homeView.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf.dataViewModel requestData:^{
            [weakSelf.homeView.tableView.mj_footer endRefreshing];
            [weakSelf.homeView.tableView reloadData];
        } fBlock:^{
            
        } isRefresh:NO];
    }];
    
}

- (void)setupUI{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.homeView];
}

- (HomeView *)homeView{
    if (_homeView == nil) {
        _homeView = [[HomeView alloc] initWithFrame:CGRectMake(0, statusHeight+navH, self.view.bounds.size.width, self.view.bounds.size.height - statusHeight- navH)];
        _homeView.tableView.delegate = self.dataViewModel;
        _homeView.tableView.dataSource = self.dataViewModel;
    }
    return _homeView;
    
    
}

- (YJDataViewModel *)dataViewModel{
    if (_dataViewModel == nil) {
        _dataViewModel = [[YJDataViewModel alloc] init];
    }
    return _dataViewModel;
}

@end
